from docx import Document
from docx.shared import Inches
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
from docx.shared import Pt

class doc_control():
    __docpath__=None
    __document__ = None
    __algo__=None


    def __init__(self, doc_path,algo):
        self.__docpath__=doc_path
        self.__document__ = Document(doc_path)
        self.__algo__=algo

    def __del__(self):
        path = self.__docpath__.split("（")
        path.pop()
        path=path[0]
        self.__document__.save(path+"(3)"+self.__algo__.__chinesename__+".docx")

    def add_algo_title(self):

        para=self.__document__.paragraphs[0]
        text=str( self.__algo__.__chinesename__)
        text.replace("_"," ")
        text= str( para.text).split("：")[0]+":"+text
        para.clear()
        run= para.add_run(text)
        run.font.size=Pt(14)

        para = self.__document__.paragraphs[3]
        para.clear()
        run = para.add_run(text=str( self.__algo__.__name__).replace(" ","_")+".py")
        run.font.name = u'宋体'
        run.font.size = Pt(12)

    def add_algo_code(self):
        para = self.__document__.paragraphs[5]
        para.clear()
        code_text = []
        path="./sorts/"+self.__algo__.__name__+".py"
        f = open(path, 'r', encoding='utf-8')
        code_text = f.read()
        f.close()
        code_text.replace('\r' or '\n', '<w:cr/>')
        code_text.replace("\t", '<w:tab/>')
        run = para.add_run(text=code_text)
        run.font.name = u'Consolas'
        run.font.size = Pt(12)

    def add_code_result(self, result_set):
        fully_result=""
        for line in result_set:
            fully_result= fully_result+line+"\n    "
        para = self.__document__.paragraphs[7]
        para.add_run(fully_result)

    def add_png(self):
        para = self.__document__.paragraphs[8]
        run = para.add_run("\n")
        png_path="./png/"+self.__algo__.__name__+".png"
        run.add_picture(png_path, width=Inches(4.5))
        para.paragraph_format.alignment = WD_PARAGRAPH_ALIGNMENT.LEFT

